package com.fick.tags;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.News;
/**
 * 前台页面新闻列表显示标签
 * 
 * 
 * **/
public class HotNewsListTag extends TagSupport {
	private static Log log = LogFactory.getLog(NewsTag.class);
	
	public int doStartTag() throws JspException {
			
		try {
			StringBuffer sb = new StringBuffer();
			
			NewsDAO newsDAO = new NewsDAO();
			String hql1 = "";
			
			int temp=0;
		
			
			hql1 = " where isAss='1' and clickNum > 50";
			String hql= "";
			
			
			hql =hql1 +" order by clickNum desc ";
			
			
			
			
			int nowpage2 = Integer.valueOf(nowpage);			//当前显示页数
			
			int pagesize2 = Integer.valueOf(pagesize);			//每页记录的条数
			


			
			
			List newsList = newsDAO.findPageByHql(hql,nowpage2,pagesize2); 
			
			

			News news = new News();
			String title = "";
			if( newsList == null )
			{
				log.error("查询失败");
				return SKIP_BODY;
			}
			int length = Integer.valueOf(title_length);
			
			int split = 1;
			try{
				split = Integer.valueOf(tr_split);
			}catch(NumberFormatException e){
				e.printStackTrace();
			}
			if( newsList.size()!=0 )
			{
				for( int i = 0 ; i< newsList.size() ; i++)
				{
					news = (News)newsList.get(i);
					title = news.getTitle();
					
					if (title.length() > length) {
						title = title.substring(0, length) + "...";
	          		}
					if(split != 1 )
					{
						if((i+1)%split!=0)
						{
							sb.append("<tr>");
						}
					}
					else
					{
						sb.append("<tr>");
					}
					sb.append("<td align='left' ");
					if(!img_class.equals(""))
					{
						sb.append(" class='"+img_class);
					}
					sb.append("'>");
					
					sb.append("<img src='"+img_src+"' ");
					if(!img_height.equals(""))
					{
						sb.append(" height='"+img_height+"' ");
					}
					if(!img_width.equals(""))
					{
						sb.append(" width='"+img_width+"' ");
					}
					sb.append("/></td><td align='left'");		//图片td结束 标题td开始
					if(!td_width.equals(""))
					{
						sb.append(" width='"+td_width+"' ");
					}
					if(!td_height.equals(""))
					{
						sb.append(" height='"+td_height+"' ");
					}
					if(!td_class.equals(""))
					{
						sb.append(" class='"+td_class+"' ");
					}
					sb.append("><a href='"+title_href+"?newid="+news.getNewId());
					
					
					
					/*if(news.getTypeid()!=null&&news.getTypeid()!=0)
					{
						sb.append("&typeid="+news.getTypeid());
					}
					if(news.getBigclassid()!=null&&news.getBigclassid()!=0)
					{
						sb.append("&bigclassid="+news.getBigclassid());
					}
					
					if(news.getSmallclassid()!=null&&news.getBigclassid()!=0)
					{
						sb.append("&smallclassid="+news.getSmallclassid());
					}*/
					sb.append("'>"+title+"</a>");
					if(news.getIsTop().trim().equals("1"))
					{
						sb.append("<span class='hongsz1'>[置顶]</span>");
					}
					sb.append("</td>");	//标题td结束 日期td开始
					if(isshowDate.equals("true"))
					{
						sb.append("<td");
						if(!date_width.equals(""))
						{
							sb.append(" width = '"+date_width+"'");
						}
						if(!date_height.equals(""))
						{
							sb.append(" height = '"+date_height+"' ");
						}
						if(!date_class.equals(""))
						{
							sb.append(" class='"+date_class+"'");
						}
						sb.append(">");
						if(!date_font_class.equals(""))
						{
							sb.append("<font class='"+date_font_class+"'>");
						}
						sb.append(news.getAddDate().toString().substring(0,10));
						if(!date_font_class.equals(""))
						{
							sb.append("</font>");
						}
						sb.append("</td>");		//日期td结束
					}
					if( split!=1 && (i+1)%split == 0 )
					{
						sb.append("</tr>");
					}
					else if(split == 1 )
					{
						sb.append("</tr>");
					}
					
				}
			}else{
				sb.append("<tr><td></td><td align='center' bgcolor='#FFFFFF'>暂无信息</td><td bgcolor='#FFFFFF'></td></tr>");
			}
			JspWriter out = pageContext.getOut();
			out.println(sb.toString());
			out.flush();
		}catch(IOException e)
		{
			log.error("标签显示错误"+e);
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
	
	private String td_width = "";		//td 宽
	
	private String td_height = "";		//td 高
	
	private String valign = "";			//td对齐方式
	
	private String align = "";			//td对齐方式
	
	private String td_class = "";		//td样式
	
	private String nowpage = "1";		//当前显示页数
	
	private String pagesize = "20";		//一页多少条记录
	
	private String img_src = "";		//头图片
	
	private String img_height = "";		//头图片长
	
	private String img_width = "";		//头图片宽
	
	private String img_class = "";		//图片td样式
	
	private String date_height = "";	//日期td长
	
	private String date_width = "";		//日期td宽
	
	private String date_class = "";		//日期td样式
	
	private String url = "";			//标题链接页面
	
	private String isshowDate = "true";	//是否显示日期
	
	private String title_href="";	//链接
	
	private String date_font_class = "";	//日期字体样式
	
	private String title_length="18";
	
	private String tr_split = "1";
	
	private String isshowTop = "1";
	
	public static Log getLog() {
		return log;
	}

	public String getTd_width() {
		return td_width;
	}

	public void setTd_width(String td_width) {
		this.td_width = td_width;
	}

	public String getTd_height() {
		return td_height;
	}

	public void setTd_height(String td_height) {
		this.td_height = td_height;
	}

	public String getValign() {
		return valign;
	}

	public void setValign(String valign) {
		this.valign = valign;
	}

	public String getTd_class() {
		return td_class;
	}

	public void setTd_class(String td_class) {
		this.td_class = td_class;
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public String getNowpage() {
		return nowpage;
	}

	public void setNowpage(String nowpage) {
		this.nowpage = nowpage;
	}

	public String getPagesize() {
		return pagesize;
	}

	public void setPagesize(String pagesize) {
		this.pagesize = pagesize;
	}

	public String getImg_src() {
		return img_src;
	}

	public void setImg_src(String img_src) {
		this.img_src = img_src;
	}

	public String getImg_height() {
		return img_height;
	}

	public void setImg_height(String img_height) {
		this.img_height = img_height;
	}

	public String getImg_width() {
		return img_width;
	}

	public void setImg_width(String img_width) {
		this.img_width = img_width;
	}

	public String getDate_height() {
		return date_height;
	}

	public void setDate_height(String date_height) {
		this.date_height = date_height;
	}

	public String getDate_width() {
		return date_width;
	}

	public void setDate_width(String date_width) {
		this.date_width = date_width;
	}

	public String getDate_class() {
		return date_class;
	}

	public void setDate_class(String date_class) {
		this.date_class = date_class;
	}

	public String getImg_class() {
		return img_class;
	}

	public void setImg_class(String img_class) {
		this.img_class = img_class;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getIsshowDate() {
		return isshowDate;
	}

	public void setIsshowDate(String isshowDate) {
		this.isshowDate = isshowDate;
	}

	public String getTitle_href() {
		return title_href;
	}

	public void setTitle_href(String title_href) {
		this.title_href = title_href;
	}

	public String getDate_font_class() {
		return date_font_class;
	}

	public void setDate_font_class(String date_font_class) {
		this.date_font_class = date_font_class;
	}

	public String getTitle_length() {
		return title_length;
	}

	public void setTitle_length(String title_length) {
		this.title_length = title_length;
	}

	public String getTr_split() {
		return tr_split;
	}

	public void setTr_split(String tr_split) {
		this.tr_split = tr_split;
	}

	public String getIsshowTop() {
		return isshowTop;
	}

	public void setIsshowTop(String isshowTop) {
		this.isshowTop = isshowTop;
	}
	
}
